//    Copyright (C) Kherty.  All rights reserved.
using System.Diagnostics.Contracts;
using System.Reflection;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;
using System.ComponentModel;

namespace OpenLS.Chart
{
    public class Surface : IOfficeXmlSerializableAs, INotifyPropertyChanged, IShape
    {
        private ExtensionElement _extension;
        private PictureOptions pictureOptions;
        private ShapeProperties spPr = new ShapeProperties();


        private int thickness;

        public ExtensionElement Extension
        {
            get { return _extension; }
            set { _extension = value; notifyPropertyChanged("Extension"); }
        }

        public int Thickness
        {
            get { return thickness; }
            set { thickness = value; notifyPropertyChanged("Thickness"); }
        }

        public ShapeProperties ShapeProperties
        {
            get { return spPr; }
            set { spPr = value; notifyPropertyChanged("ShapeProperties"); }
        }

        public PictureOptions PictureOptions
        {
            get { return pictureOptions; }
            set { pictureOptions = value;notifyPropertyChanged("PictureOptions"); }
        }

        #region IOfficeXmlSerializableAs Members

        void IOfficeXmlSerializableAs.ReadXmlAs(string name, ReadContext context)
        {
            using (var c = context.Read(name))
            {
                //Thickness = c.ReadOptionalUIntegerAsInteger("thickness", 0);
                Thickness = (int)c.ReadOptionalElementUIntegerAttribute("thickness").GetValueOrDefault();
                ShapeProperties = c.ReadOptionalElement<ShapeProperties>("spPr");
                pictureOptions = c.ReadOptionalElement<PictureOptions>("pictureOptions");
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }

        void IOfficeXmlSerializableAs.WriteXmlAs(string name, WriteContext context)
        {
            using (WriteContext c = context.Write(name))
            {
                //c.WriteOptionalUIntegerAsInteger("thickness", Thickness, 0);
                c.WriteOptionalElementUIntegerAttribute("thickness", "val", (uint)Thickness, 0);
                c.WriteOptionalElement(ShapeProperties);
                c.WriteOptionalElement(pictureOptions);
                c.WriteOptionalElement(Extension);
            }
        }

        #endregion

        //\\extLst

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        void notifyPropertyChanged(string propertyName)
        {
            Contract.Requires(GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) != null);
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}